const { ipcMain } = require("electron");
const { createWin } = require("./share");
const path = require("path");

const createtitviewWin = () => {
    const titviewWin = createWin({
        width: 200,
        height: 50,
        webPreferences: {
            nodeIntegration: true, // 允许预加载js文件使用require模块
            preload: path.join(__dirname, "../src/view/title/preload.js"),
        },
        transparent: true
    }, "./src/view/title/index.html");
    return titviewWin;
};
const createIpcTit = () => {
    let titleWin
    ipcMain.on("open-titleview", () => {
        titleWin = createtitviewWin();

        titleWin.setAlwaysOnTop(true);

        // 监听窗口关闭
        titleWin.on('closed', () => {
            titleWin = null
        })
    });

    ipcMain.on("close-titleview", () => {
        titleWin.close();
    });
    
    // 监听修改标题事件
    ipcMain.on('update-title', (event, data) => {
        titleWin.webContents.send('revise-title', data)
    })
}

module.exports = {
    createIpcTit
}